package com.igirl.manage.service;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igirl.manage.pojo.BasePojo;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
import java.lang.reflect.ParameterizedType;

/**
 * @Author: jiaheng
 * @Description:
 * @Date: Created in 20:52 2017/12/22
 * @Modified:
 */
public abstract class BaseService<T extends BasePojo> {

    @Autowired
    public Mapper<T> mapper;

    public Mapper<T> getMapper() {
        return this.mapper;
    }

    public void setMapper(Mapper<T> mapper) {
        this.mapper = mapper;
    }

    // 当前Service上泛型的字节码对象
    private Class<T> clazz;
    {
        // 读取当前类上的泛型
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz = (Class<T>) type.getActualTypeArguments()[0];
    }

    /**
     * 根据主键进行查询
     * @param id
     * @return
     */
    public T queryById(Long id){
        return this.getMapper().selectByPrimaryKey(id);
    }

    /**
     * 查询全部
     * @return
     */
    public List<T> queryAll(){
        return this.getMapper().select(null);
    }


    public int queryCount(T recod) {
        return this.getMapper().selectCount(recod);
    }
    /**
     * 根据条件查询1条数据。
     * @param record
     * @return
     */
    public T queryOne(T record){
        return this.getMapper().selectOne(record);
    }

    /**
     * 根据条件查询多条数据
     * @param record
     * @return
     */
    public List<T> queryListByWhere(T record){
        return this.getMapper().select(record);
    }

    /**
     * 根据条件分页查询
     * @param record
     * @return
     */
    public PageInfo<T> queryPageListByWhere(T record, Integer page, Integer rows){
        PageHelper.startPage(page, rows);
        List<T> list = this.getMapper().select(record);
        return new PageInfo<>(list);
    }

    /**
     * 插入数据
     * @param record
     * @return
     */
    public int save(T record){
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return this.getMapper().insert(record);
    }

    /**
     * 插入数据,只操作record中的非空属性
     * @param record
     * @return
     */
    public Integer saveSelective(T record){
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return this.getMapper().insertSelective(record);
    }


    /**
     * 更新数据
     * @param record
     * @return
     */
    public Integer update(T record){
        record.setUpdated(new Date());
        // TODO 这里如何解决用户传入的created属性的问题？
        return this.getMapper().updateByPrimaryKey(record);
    }

    /**
     * 更新数据,只操作record中的非空属性
     * @param record
     * @return
     */
    public Integer updateSelective(T record){
        record.setUpdated(new Date());
        // 保证created字段不会被修改
        record.setCreated(null);
        return this.getMapper().updateByPrimaryKeySelective(record);
    }

    /**
     * 根据主键进行删除
     * @param id
     * @return
     */
    public Integer deleteById(Long id){
        return this.getMapper().deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public Integer deleteByIds(String property, List<Object> ids){
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, ids);
        return this.getMapper().deleteByExample(example);
    }

    /**
     * 根据条件删除
     * @param record
     * @return
     */
    public Integer deleteByWhere(T record){

        return this.getMapper().delete(record);
    }

    /**
    * order是排序条件
    *
    * @Author: jiaheng
    * @Date: 10:34 2017/12/31
    * @Params: [order, example]
    * @Return: T
    */
    public List<T>  queryByExampleOrder(Example example) {
        return this.getMapper().selectByExample(example);
    }
}
